package com.project.controller;

import com.project.bean.InCarBean;
import com.project.bean.StorageBean;
import com.project.bean.UserBean;
import com.project.service.IInCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("inCar")
public class InCarController {
    @Autowired
    private IInCarService service;

    @RequestMapping("add")
    public String add(StorageBean storage, String opt, Integer userId, Integer addNum) {
        service.add(storage, opt, userId, addNum);
        return "ok";
    }


    @RequestMapping("findByUserId")
    public List<InCarBean> findByUserId(HttpServletRequest req) {
        UserBean userBean = (UserBean) req.getSession().getAttribute("user");

        return service.findByUserId(userBean.getId());
    }


    @RequestMapping("update")
    public String update(Integer carId, Integer newPrice, Integer newNum) {
        service.update(carId, newPrice, newNum);

        return "ok";
    }

    ;

    @RequestMapping("del")
    public String del(Integer carId) {
        service.del(carId);
        return "ok";
    }

    ;

    @RequestMapping("joinStorage")
    public String joinStorage(String supName, String person, HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("user");
        service.joinStorage(supName, person, user.getId());
        return "ok";
    }


}
